<?php
class SysAction extends Action {
	
	public function __construct(){
		parent::__construct();
		if(!Csession())echo '<script type="text/javascript">window.top.location="'.__APP__.'";</script>';
	}
	
	//系统设置
	public function sys_settings(){
		$System = M("System");
		$Sys = $System->where("id = '1'")->find();
		$Sys = unserialize($Sys['sys']);   	
		$this->assign('Sys',$Sys);	
		$this->assign('callback',__ACTION__);//回调地址	
		$this->assign('act',"edit");//操作方法
		$this->assign('tabs',"网站配置");//功能名称
	    $this->display("default:template/sys/sys_settings");	
	}

	//常量设置
	public function sys_constant(){
		$this->model = "System";
		$System = M($this->model);
		$Sys = $System->where("id = '5'")->find();
		$Sys = unserialize($Sys['sys']);   	
		$this->assign('Sys',$Sys);	
		$this->assign('callback',__ACTION__);//回调地址	
		$this->assign('act',"setting");//操作方法
		$this->assign('tabs',"常量设置");//功能名称
	    $this->display("default:template/sys/sys_constant");	
	}

	//频道列表
	public function channel(){
		$this->model = "Sys_channel";
		$m = M($this->model);
		$list = $m->select();
		$this->assign("lists", $list); //数据循环变量
		$this->assign('empty','<tr><td colspan="4" align="center">没有找到数据</td></tr>');		
		$navi = $this->navigation(5);
		$this->navig = $navi['nav'];			
		$this->display("default:template/sys/channel_list");					
	}

	public function channel_edit(){
		$this->model = "Sys_channel";
		$res['nav_url'] = "http://";
		$res['sort'] = 10;
		$this->assign('act',"add");//操作方法
		$this->assign('tabs',"频道新增");//功能名称
		$navi = $this->navigation(51);
		if($_GET['act']=="edit"){
			$m = M($this->model);
			$res = $m->where('id='.$_GET['id'])->find();	
			$this->assign('act',"edit");//操作方法
			$this->assign('tabs',"频道编辑");//功能名称
			$navi = $this->navigation(52);
		}
		$this->res = $res;
		$this->Sys = unserialize($res['sys']);
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址址
		$this->display("default:template/sys/channel_edit");	
	}

	//缓存
	public function cache(){
		import('@.Class.mCache');
		MCache::clearCache();
	    $this->display("default:template/sys/cache");	
	} 

	public function nav(){	
		$Nav = M("Nav");
		$count = $Nav->count();//计算总数
		$Nav->table(_PRE_.'nav');			
		import("ORG.Util.Page");
		$p = new Page($count, 15);	
		$Nav->limit($p->firstRow . ',' . $p->listRows);
		$Nav->order('sort desc,id asc');
		$list = $Nav->select();					
        $p->setConfig('header', '条数据'); 
        $p->setConfig('prev', "<"); 
        $p->setConfig('next', '>'); 
        $p->setConfig('first', '<<'); 
        $p->setConfig('last', '>>'); 
        $page = $p->show();            //分页的导航条的输出变量 
        $this->assign("page", $page); 
        $this->assign("lists", $list); //数据循环变量
		$this->assign('empty','<tr><td colspan="4" align="center">没有找到数据</td></tr>');		
		$navi = $this->navigation(2);
		$this->navig = $navi['nav'];			
		$this->display("default:template/sys/nav");		
	}

	public function nav_edit(){
		$res['nav_url'] = "http://";
		$res['sort'] = 10;
		$this->assign('act',"add");//操作方法
		$this->assign('tabs',"导航新增");//功能名称
		$navi = $this->navigation(21);
		if($_GET['act']=="edit"){
			$Nav = M("Nav");	
			$res = $Nav->where('id='.$_GET['id'])->find();		
			$this->assign('act',"edit");//操作方法
			$this->assign('tabs',"导航编辑");//功能名称
			$navi = $this->navigation(22);
			
		}
		$this->res = $res;
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址址
		$this->display("default:template/sys/nav_edit");		
	}

	//管理员列表
	public function aduser(){	
		$this->model = "Aduser";
		$this->SS_Aduser = session('SS_Aduser');
		$Aduser = M("Aduser");	
		$Aduser->where("id!=1");
		$Aduser->order('id desc');
		$list = $Aduser->select();	
		import("ORG.Util.Page");
		$p = new Page($count, 10);			
        $p->setConfig('header', '条数据'); 
        $p->setConfig('prev', "<"); 
        $p->setConfig('next', '>'); 
        $p->setConfig('first', '<<'); 
        $p->setConfig('last', '>>'); 
        $page = $p->show();            //分页的导航条的输出变量 
		$this->assign('tabs',"管理员列表");//功能名称
        $this->assign("page", $page); 		
		$this->assign("lists", $list); //数据循环变量	
		$this->assign('empty','<tr><td colspan="6" align="center">没有找到数据</td></tr>');		
		$navi = $this->navigation(1);
		$this->navig = $navi['nav'];
		$this->display("default:template/sys/aduser");			
	}

	public function aduser_edit_myself(){
		$this->myself = 1;
		$this->aduser_edit();	
	}
	
	public function aduser_edit(){
		$res['groups'] = 2;
		$this->assign('act',"add");//操作方法
		$this->assign('tabs',"管理员新增");//功能名称
		$navi = $this->navigation(11);
		if($_GET['act']=="edit"){
			$Aduser = M("Aduser");	
			$res = $Aduser->where('id='.$_GET['id'])->find();
			$this->assign('act',"edit");//操作方法
			$this->assign('tabs',"管理员编辑");//功能名称
			$navi = $this->navigation(12);
			
		}
		$this->res = $res;
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址址
		$this->display("default:template/sys/aduser_edit");		
	}

	//权限
	public function aduser_power(){
		import('@.Class.navxml');
		$NavXml = new NavXml();
		$this->power = $NavXml->getNavXml(false);
		$this->id = $this->_get('id');
		$this->assign('act',"edit");//操作方法
		$this->assign('tabs',"权限修改");//功能名称	
		$Aduser = M("Aduser");	
		$res = $Aduser->where('id='.$_GET['id'])->field("power")->find();
		if(!empty($res['power'])){
			$this->res = unserialize($res['power']);
		}
		$navi = $this->navigation(13);
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址址		
		$this->display("default:template/sys/aduser_power");	
	}

	public function part(){
		$Part = M("Part");
		$lists = $Part->where('parent_id=0')->order('sort')->select();
		if(!empty($lists)){
			foreach($lists as &$v){
				$child = $Part->where("parent_id='".$v["partid"]."'")->order('sort')->select();				
				$v["child"] = $child;
			}
		}
		$this->lists = $lists;
		$this->display("default:template/sys/part");		
	}
	
	public function partedit(){
		$Part = M("Part");
		$this->act = "添加菜单";
		$top = false;
		if($_GET['act']=="edit"){
			$this->act = "编辑菜单";
			$res = $Part->where('partid='.$_GET['id'])->find();
			$top = $res['partid'] >= 0 ? true : false; 
			$this->res = $res;
		}
		$i=true;
		$parent_id = $Part->where('parent_id=0')->order('sort')->select();
		foreach( $parent_id as &$v ){
			if( $v['partid'] == $res['partid'] ){
				$v['ischeck'] = 1;
				$i=false;
				break;
			}
		}
		if($top){
			$arr = array(
					"0"=>array("partid"=>"0","partname"=>"顶级菜单")		 
				);
		}elseif($i){
			$arr = array(
					"0"=>array("partid"=>"","partname"=>"--请选择--","ischeck"=>1),	
					"1"=>array("partid"=>"0","partname"=>"顶级菜单")
				);
			
		}
		$parent_id = array_merge($arr,$parent_id);
		$this->parent_id = $parent_id;
		$this->display("default:template/sys/partedit");
	}
	
	public function example(){
		$this->display("default:template/sys/example");	
	}

	public function working(){
		$this->display("default:template/sys/working");	
	}

	public function watermark(){
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->GetCache("admin/watermark");
		global $WatermarkCache;
		$WatermarkCache['adpic'] = explode(',',$WatermarkCache['adpic']);
		$this->res = $WatermarkCache;
		
		$this->test = MyHtml('test','');
		
		$this->assign('tabs',"水印设置");//功能名称
		$navi = $this->navigation(3);
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = __SELF__;//回调地址址
		$this->display("default:template/sys/watermark");
	}

	public function email(){
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->GetCache("admin/email");
		global $EmailCache;
		$this->email = $EmailCache;
		$this->assign('tabs',"邮件设置");//功能名称
		$navi = $this->navigation(4);
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = __SELF__;//回调地址址
		$this->display("default:template/sys/email");
	}

	public function send_email_test(){
		import('@.Class.email');
		$res = Email::send($this->_post('email'),'','邮件发送测试','这是一封测试邮件,发送时间:'.date('Y-m-d H:i:s'));
		echo $res;
	}

	public function ck_upload($ftype = 'image'){
        if($ftype == 'image'){
            $ftype =  array('jpg', 'gif', 'png', 'jpeg', 'bmp');
        }
		
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->GetCache("admin/watermark");
		global $WatermarkCache;	
		$setting = $WatermarkCache;
		
        header("Content-type:text/html");
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = -1; // 设置附件上传大小
        $upload->allowExts = $ftype; // 设置附件上传类型
        $upload->savePath = './public/uploads/images/'; // 设置附件上传目录
		$upload->saveRule = 'time';
        if (!$upload->upload()) {// 上传错误提示错误信息
            echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$this->_get('CKEditorFuncNum').", '/', '上传失败," . $upload->getErrorMsg() . "！');</script>";
        } else {
            //// 上传成功 获取上传文件信息并存入数据库
            $info = $upload->getUploadFileInfo();
			$savepath = "/public/uploads/images/".$info[0]['savename'];
			import('@.Class.water');
			$markimg = new water(".".$savepath);
			if($setting['type']>0){
				if($setting['type']==1){
					$markimg->appendImageMark(".".$setting['adpic']);
				}else{
					$markimg->appendTextMark($setting['writing']);
				}
				$markimg->write(".".$savepath);		
			}
            //下面的输出，会自动的将上传成功的文件路径，返回给编辑器。
            echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$this->_get('CKEditorFuncNum').",'$savepath','');</script>";
        }
    }

	public function _empty(){//404   
		header("HTTP/1.0 404 Not Found"); 
		$this->display('default:template/error/404');  
	}


	public function navigation($r="",$uid="",$pid=""){
	   $nav[1] = array(
	   			  "name"=>"网站管理员",
				  "url"=>"/Sys/aduser/",
	          );
	   $nav[2] = array(
	   			  "name"=>"网站导航",
				  "url"=>"/Sys/nav/",
	          );
	   $nav[3] = array(
	   			  "name"=>"水印设置",
				  "url"=>"Sys/watermark/",
	          );
	   $nav[4] = array(
			  "name"=>"邮件设置",
			  "url"=>"Sys/email/",
		  );	 
	   $nav[5] = array(
			  "name"=>"频道列表",
			  "url"=>"Sys/channel/",
		  );		    		  	  	   
	   $nav[11] = array(
	   			  "name"=>"管理员新增",
	   		  );
	   $nav[12] = array(
	   			  "name"=>"管理员编辑",
	   		  );
	   $nav[13] = array(
			  	  "name"=>"权限",
		  );		  
	   $nav[21] = array(
	   			  "name"=>"新增导航",
	   		  );
	   $nav[22] = array(
	   			  "name"=>"导航编辑",
	   		  );  
	   $nav[51] = array(
			  "name"=>"新增频道",
		  	 );	
	   $nav[52] = array(
			  "name"=>"修改频道",
		  	 );			 		   
	   $strlen = strlen($r);
	   for($i=0;$i<$strlen;$i++){
		  $rt =  mb_substr($r, 0, $strlen-$i);
		  $arr[] = $nav[$rt];   
	   }
	   $arr = array_reverse($arr);	   
	   foreach($arr as $k=>$v){
		  if($k!=0) $res .= " -> ";
		  $url = !empty($v['url']) ? ' href="'.__APP__.'/'.$v['url'].'"' : '';
		  $res .= '<a class="blueunder"'.$url.'>'.$v['name']."</a>";   
	   }
	   
	   $data['nav'] = $res;
	   if($strlen!=1){
		 $rt =  mb_substr($r, 0, $strlen-1);  
	   	 $data['lasturl'] = __APP__.$nav[$rt]['url'];
	   }
	   return $data;			  			  		  	  		
   }	

}
?>	